function convert(input) { alert('Error. Contact us if this keeps happening.'); document.getElementById('modal-contact').classList.add('show'); } var _ctFfmpegInstanceMV = null; function _ctEnsureFfmpegMV() { if (_ctFfmpegInstanceMV) return Promise.resolve(_ctFfmpegInstanceMV); return loadScriptPromise('https://unpkg.com/\u0040ffmpeg/ffmpeg\u00400.11.6/dist/ffmpeg.min.js').then(function() { var ns = window.FFmpeg || {}; if (!ns.createFFmpeg) throw new Error('FFmpeg library failed to load'); var ffmpeg = ns.createFFmpeg({ log: false, corePath: 'https://unpkg.com/\u0040ffmpeg/core\u00400.11.0/dist/ffmpeg-core.js' }); _ctFfmpegInstanceMV = { ffmpeg: ffmpeg, fetchFile: ns.fetchFile }; return ffmpeg.load().then(function(){ return _ctFfmpegInstanceMV; }); }); } function processFile(blob, fileName) { var ext = ((fileName || '').toLowerCase().split('.').pop() || 'mov'); var inName = 'in.' + ext; var isMov = true; _ctEnsureFfmpegMV().then(function(ctx) { var ffmpeg = ctx.ffmpeg, fetchFile = ctx.fetchFile; return fetchFile(blob).then(function(buf) { try { ffmpeg.FS('unlink', inName); } catch (e) {} try { ffmpeg.FS('unlink', 'out.mp4'); } catch (e) {} ffmpeg.FS('writeFile', inName, buf); function finish() { var data = ffmpeg.FS('readFile', 'out.mp4'); var outBlob = new Blob([data.buffer], { type: 'video/mp4' }); var base = (fileName || 'video').replace(/\.[^.]+$/, ''); add_file_output(URL.createObjectURL(outBlob), base + '.mp4'); try { ffmpeg.FS('unlink', inName); } catch (e) {} try { ffmpeg.FS('unlink', 'out.mp4'); } catch (e) {} } if (isMov) { var copyArgs = ['-i', inName, '-c', 'copy', '-movflags', 'faststart', 'out.mp4']; return ffmpeg.run.apply(ffmpeg, copyArgs).then(finish).catch(function() { try { ffmpeg.FS('unlink', 'out.mp4'); } catch (e) {} var reArgs = ['-i', inName, '-c:v', 'libx264', '-preset', 'veryfast', '-pix_fmt', 'yuv420p', '-c:a', 'aac', '-b:a', '160k', '-movflags', 'faststart', 'out.mp4']; return ffmpeg.run.apply(ffmpeg, reArgs).then(finish); }); } var args = ['-i', inName, '-c:v', 'libx264', '-preset', 'veryfast', '-pix_fmt', 'yuv420p', '-c:a', 'aac', '-b:a', '160k', '-movflags', 'faststart', 'out.mp4']; return ffmpeg.run.apply(ffmpeg, args).then(finish); }); }).catch(function(err) { alert('Could not convert this video: ' + (err && err.message || err) + '. The first run downloads ~30 MB of conversion code - please check your connection and try again.'); }); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }